
import os


def geneMakefile(dirname):
    filearr = os.listdir(dirname)


    with open(dirname + "/makefile", "w") as f:
        f.write("SRC = $(wildcard *.s)\n")
        f.write("OBJ = $(SRC:%.s=%.o) \n")
        f.write("BIN = $(OBJ:%.o=%)\n")
        f.write('''
AS = as
CC = ld

LDFLAGS = -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -lc
ASFLAGS = --32

clean : 
\trm $(OBJ)
cleanall : 
\trm $(BIN) $(OBJ)
''')

        #compile
        if len(filearr) > 0:
            f.write("{} : {}\n\t$(AS) $(ASFLAGS) -o $@ $<\n".format(filearr[0][:-1] + "o", filearr[0]))
            if len(filearr) > 1:
                for name in filearr[1:]:
                    f.write("{} : {}\n".format(name[:-1]+"o", name))

        f.write("\n")

        #link
        if len(filearr) > 0:
            f.write("{} : {}\n\t$(CC) $(LDFLAGS) -o $@ $<\n".format(filearr[0][:-2], filearr[0][:-1] + "o"))
            if len(filearr) > 1:
                for name in filearr[1:]:
                    f.write("{} : {}\n".format(name[:-2], name[:-1] + "o"))

        f.write("all : ")
        for name in filearr:
            f.write(name[:-2]+" ")
        f.write("clean")
        f.write('\n')

if __name__ == "__main__":
    geneMakefile(input())





